package db

import (
	"github.com/gookit/color"
	"go.mongodb.org/mongo-driver/mongo"
	"gorm.io/gorm"
	"log"
	"utilsx/db/mongoUtil"
)

func Begin(tx *gorm.DB, handle func(x *gorm.DB) error) error {
	err := handle(tx)
	if err != nil {
		tx.Rollback()
		log.Println(err)
	} else {
		tx.Commit()
	}
	return err
}

func InitMongo(config DataBase) *mongo.Database {
	con, err := mongoUtil.CreatCon(config.Username, config.Password, config.Host)
	if err != nil {
		color.Redln("NewMongoDB Error")
		panic(err)
	}
	return con.Database(config.DataBase)
}
